﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2016-2018 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Split interaction</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="an OL3 interaction to draw regular polygon" />
  <meta name="keywords" content="ol3, interaction, draw, regular, rectangle, circle, triangle" />

  <link rel="stylesheet" href="../style.css" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Split interaction</h1>
  </a>
  <div class="info">
    <b>ol/interaction/ModifyFeature</b> is an interaction for modifying feature geometries, 
    similar to the core <i>ol/interaction/Modify</i>.
    <br/>
    The interaction is more suitable to use to handle feature modification: 
    only features concerned by the modification are passed to the events 
    (instead of all feature with ol/interaction/Modify)
    <ul>
      <li>
        the <i>modifystart</i> event is fired before the feature is modified (no point still inserted)
      </li>
      <li>
        the <i>modifyend</i> event is fired after the modification
      </li>
      <li>
        it fires a <i>modifying</i> event in beetween.
      </li>
    </ul>
    For a more complete integration with undo/redo, see <a href="./map.interaction.undoredo.html">undo/redo example</a>.
    </div>

  <!-- Map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options" >
    <h2>Options:</h2>
    <ul><li>
      Operation: 
      <input type="radio" name="op" value="draw" /><label> Draw</label>
      <input type="radio" name="op" value="modify" /><label>Modify</label>
      <input type="radio" name="op" value="modifyfeature" checked="checked" /><label>ModifyFeature</label>
    </li></ul>
    <div style="background:white; padding:0 0.45em;"><span id="info"></span>&nbsp;</div>
    <p class='modifystart' style='margin:0; font-size: .8em; color:green'></p>
    <p class='modifying' style='margin:0; font-size: .8em; color:gray'></p>
    <p class='modifyend' style='margin:0; font-size: .8em; color:red'></p>
  </div>
  

  <script type="text/javascript">
    // Layers
    var layers = [
      new ol.layer.Tile({
        title:'terrain-background',
        source: new ol.source.Stamen({ layer: 'terrain' })
      })
    ];
    // The map
    var map = new ol.Map({
      target: 'map',
      view: new ol.View({
        zoom: 5,
        center: [261720, 5951081]
      }),
      controls: ol.control.defaults({ "attribution": false }),
      layers: layers
    });

    // Style function
    function getStyle(f) {
      return [
        new ol.style.Style({
          stroke: new ol.style.Stroke({ color: '#ffcc33',width: 2 }),
          fill: new ol.style.Fill({ color: [255,255,255,.5] })
        }),
        new ol.style.Style({
          image: new ol.style.RegularShape({ radius: 4, points:4, fill: new ol.style.Fill({ color: '#f00' }) })
        })
      ]
    }

    // New vector layer
    var vector = new ol.layer.Vector({
      name: 'Vecteur',
      source: new ol.source.Vector({ features: new ol.Collection() }),
      style: getStyle
    })
    map.addLayer(vector);
    vector.getSource().addFeature(new ol.Feature(new ol.geom.Point([23000, 5500000])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.MultiPoint([[100000, 5400000], [200000, 5400000]])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.LineString([[-288626, 5757848], [210354, 5576845], [34243, 6305749]])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.Polygon([
      [[400000, 5600000], [680000, 5710000], [690000, 6140000], [420000, 6180000],[400000, 5600000]],
      [[500000, 5750000], [520000, 6000000], [580000, 5900000], [500000, 5750000]]
    ])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.MultiPolygon([
      [[[690000, 6300000], [700000, 6400000], [600000, 6400000], [690000, 6300000]]],
      [[[490000, 6300000], [500000, 6400000], [400000, 6400000], [490000, 6300000]]]
    ])));
    vector.getSource().addFeature(new ol.Feature(new ol.geom.MultiLineString([
      [[300000, 6300000], [200000, 6400000], [100000, 6400000]],
      [[300000, 6500000], [200000, 6600000], [100000, 6600000]],
    ])));

    // Test using geometry collection instead
    function toGeometryCollection () {
      var geom = [];
      vector.getSource().getFeatures().forEach(function(f) {
        geom.push(f.getGeometry());
      });
      vector.getSource().clear();
      vector.getSource().addFeature(new ol.Feature(new ol.geom.GeometryCollection(geom)));
    }

    var interactions = {
      draw: new ol.interaction.Draw({
        source: vector.getSource(),
        type: "LineString"
      }),
      modify: new ol.interaction.Modify ({
        source: vector.getSource(),
        // insertVertexCondition: function(){ return false; }
      }),
      modifyfeature: new ol.interaction.ModifyFeature ({ 
        sources: vector.getSource(),
        // insertVertexCondition: function(){ return false; }
        // style: getStyle
      })
    }
    for (var i in interactions) map.addInteraction(interactions[i]);

    setInteraction = function (){
      $(".options > div").html("");
      var name = $('[name="op"]:checked').val();
      for (var i in interactions) {
        interactions[i].set("active", (i==name));
      }
    }
    setInteraction();

    $("label").click(function(){$(this).prev().click();});
    $("input:radio").on("change",setInteraction);

    // Add snap to get multi modification
    map.addInteraction(new ol.interaction.Snap({ 
      source: vector.getSource(), 
      pixelTolerance: 5 
    }));

    // Listen modifications
    interactions.modifyfeature.on(['modifystart', 'modifyend', 'modifying'], function(e) {
      // First modify feature
      var f = e.features[0];
      if (e.type==='modifystart') $('.options p').html('');
      var info = e.type + ' '+ (e.features.length||'??') + ' feature(s) : '; 
      info += (f ? f.getGeometry().getType() : '');
      if (f && f.getGeometry().flatCoordinates) info += ' '+(f ? (f.getGeometry().flatCoordinates.length/2) : '?') + ' point(s)'
      $('.options .'+e.type).text(info);
    });
    interactions.modify.on(['modifystart', 'modifyend', 'modifying'], function(e) {
      // Try to get the modified features
      var f = interactions.modify.getModifiedFeatures()[0];
      if (e.type==='modifystart') $('.options p').html('');
      var info = e.type + ' '+ (e.features.length||'??') + ' feature(s) : '; 
      info += (f ? f.getGeometry().getType() : '');
      if (f && f.getGeometry().flatCoordinates) info += ' '+(f ? (f.getGeometry().flatCoordinates.length/2) : '?') + ' point(s)'
      $('.options .'+e.type).text(info);
    });
    
  </script>

</body>
</html>